package com.hugo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hugo.eduservice.entity.EduCourse;
import com.hugo.eduservice.entity.EduCourseDescription;
import com.hugo.eduservice.entity.EduVideo;
import com.hugo.eduservice.entity.vo.CourseInfoVo;
import com.hugo.eduservice.entity.vo.CoursePublishVo;
import com.hugo.eduservice.mapper.EduCourseMapper;
import com.hugo.eduservice.service.EduChapterService;
import com.hugo.eduservice.service.EduCourseDescriptionService;
import com.hugo.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hugo.eduservice.service.EduVideoService;
import com.hugo.serviceconfig.exceptionhandler.HugoException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hugo
 * @since 2020-09-08
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    //添加课程信息
    @Override
    public String saveCourseInfoVo(CourseInfoVo courseInfoVo) {
        //向课程表里添加课程的基本信息
        //CourseInfoVo对象转换为eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);

        if(insert ==0){
            //添加失败
            throw  new HugoException(20001,"添加失败");
        }
        //获取课程id
        String cid = eduCourse.getId();

        //课程简介表里添加简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置课程id
        courseDescription.setId(cid);
        eduCourseDescriptionService.save(courseDescription);
        return cid;

    }

    //查询课程信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);


        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;

    }

    //修改课程
    @Override
    public void UpdateCourseInfo(CourseInfoVo courseInfoVo) {

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if(i == 0){
            throw new HugoException(200001,"修改失败");
        }

        EduCourseDescription eduCourseDescription =new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用Mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        // 根据id删除 小节
        eduVideoService.removeVideoById(courseId);
        //根据id删除章节
        eduChapterService.removeChapterById(courseId);
        //根据id删除描述
        eduCourseDescriptionService.removeById(courseId);
        //删除课程本身
        baseMapper.deleteById(courseId);


    }
}
